package com.jiguang.Electricvehicle;

import static com.jiguang.Electricvehicle.fragment.HomeFragment.REQUEST_ENABLE_BT;
import static com.jiguang.Electricvehicle.fragment.HomeFragment.REQUEST_LOCATION_PERMISSION;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.huawei.hms.hmsscankit.ScanKitActivity;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions;
import com.jiguang.Electricvehicle.fragment.BaseSimpleFragment;
import com.jiguang.Electricvehicle.fragment.HomeFragment;
import com.jiguang.Electricvehicle.fragment.SecondFragment;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import butterknife.BindView;
import me.yokeyword.fragmentation.SupportFragment;

public class MainActivity extends BaseSimpleActivity {
    public static final int CAMERA_REQ_CODE = 111;
    public static final int DECODE = 1;
    private static final int REQUEST_CODE_SCAN_ONE = 0X01;

    private SupportFragment[] mFragments = new SupportFragment[2];
    private String[] mTitles = {"爱车", "设置"};
    private int[] mIconUnselectIds = {
            R.mipmap.icon_home_unselect, R.mipmap.icon_set_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.icon_home_select, R.mipmap.icon_set_select};
    @BindView(R.id.bottomBar)
    BottomBar mBottomBar;

    @Override
    protected void onViewCreated() {
        //设置状态栏主题色
        TranslucentCompat.transparencyBar(this);
        TranslucentCompat.statusBarLightMode(this);
        initFragments();
        initBottomMenu();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initEventAndData(Bundle extras) {

    }

    /**
     * 处理Menu缓存数据,初始化BottomMenu
     */
    private void initBottomMenu() {
        for (int i = 0; i < mTitles.length; i++) {
            mBottomBar.addItem(new BottomBarTab(this, mIconSelectIds[i], mIconUnselectIds[i], mTitles[i]));
        }
        mBottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                if (position == 1) {
                    requestPermission(CAMERA_REQ_CODE, DECODE);
                    /*if (AndPermission.hasPermissions(MainActivity.this, Permission.Group.CAMERA)) {
                        ScanUtil.startScan(MainActivity.this, REQUEST_CODE_SCAN_ONE, new HmsScanAnalyzerOptions.Creator().create());

//                        startActivityForResult(new Intent(MainActivity.this, ChargeQrActivity.class), 100);
                    } else {
                        AndPermission.with(MainActivity.this)
                                .runtime()
                                .permission(Permission.Group.CAMERA)
                                .rationale((context1, data, executor) -> {
                                    executor.execute();
                                })
                                .onGranted(permissions -> {
                                    startActivityForResult(new Intent(MainActivity.this, ChargeQrActivity.class), 100);
                                })
                                .onDenied(permissions -> {
                                    ToastUtil.toastShortMessage("请先开启相机权限");
                                })
                                .start();
                    }*/
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
        mBottomBar.setCurrentItem(0);

    }

    HomeFragment homeFragment;

    private void initFragments() {
        //获取管理者
//        FragmentManager supportFragmentManager = getSupportFragmentManager();
//        //开启事务
//        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        homeFragment = newInstance(HomeFragment.class);
        mFragments[0] = homeFragment;
//        SupportFragment secondFragment = newInstance(SecondFragment.class);
//        mFragments[1] = secondFragment;
        loadMultipleRootFragment(R.id.fl_container, 0, homeFragment);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mBottomBar != null) {
            mBottomBar.setCurrentItem(0);
        }
    }

    //编辑请求权限
    private void requestPermission(int requestCode, int mode) {
        ActivityCompat.requestPermissions(
                this,
                new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.READ_EXTERNAL_STORAGE},
                requestCode);
    }

    //权限申请返回
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (permissions == null || grantResults == null) {
            return;
        }

        if (grantResults.length < 2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (requestCode == CAMERA_REQ_CODE) {
            startActivityForResult(new Intent(MainActivity.this, QrActivity.class), 1003);
//            ScanUtil.startScan(this, REQUEST_CODE_SCAN_ONE, new HmsScanAnalyzerOptions.Creator().create());
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 100) {
            Log.d("接收---", data.getExtras().getString("stubId"));
            homeFragment.setStubId(data.getExtras().getString("stubId"));
        }

        if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_OK) {
            //蓝牙已经开启
            homeFragment.blueOpenWithRequestLocation();
        }
        if (requestCode == 1003) {
            Log.d("接收---", data.getExtras().getString("stubId"));
            homeFragment.setStubId(data.getExtras().getString("stubId"));
        }

        if (requestCode == REQUEST_CODE_SCAN_ONE) {
            HmsScan obj = data.getParcelableExtra(ScanUtil.RESULT);
            if (obj != null) {
                homeFragment.setStubId(obj.originalValue);
            }
        }
    }

//    private boolean doubleBackToExitPressedOnce = false;

//    @Override
//    public void onBackPressedSupport() {
//        if (doubleBackToExitPressedOnce) {
//            super.onBackPressed();
//            return;
//        }
//
//        this.doubleBackToExitPressedOnce = true;
//        Toast.makeText(this, "请再按一次返回键退出应用", Toast.LENGTH_SHORT).show();
//
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                doubleBackToExitPressedOnce = false;
//            }
//        }, 2000);
//    }

}